//
//  FJSelMaterialsView.swift
//  FruitJuice
//
//  Created by Rui on 2023/12/22.
//

import UIKit
import SwiftEntryKit

class FJSelMaterialsView: UIView, SAAnimationDelegate, SAAnimationProtocol {

    func animationContainerViewSize() -> CGSize {
        CGSize(width: .screenWidth, height: 350.0)
    }
    
    func animationContainerViewStyle() -> SAAnimationViewStyle {
        .bottom
    }
    
//    func animationContainerScreenInteraction() -> EKAttributes.UserInteraction {
//        .dismiss
//    }
    var type = 1 // 1.fruit  2.green
    private let collectionView = UICollectionView(frame: .zero, collectionViewLayout: UICollectionViewFlowLayout())
    private var fruitArray: [FJPushMaterialModel] = {
        var arr = [FJPushMaterialModel]()
        for index in 1 ..< 38 {
            let img = "FJ_Fruit_\(index)"
            let model = FJPushMaterialModel(iconImage: img, select: false)
            arr.append(model)
        }
        return arr
    }()
    private var greenArray: [FJPushMaterialModel] = {
        var arr = [FJPushMaterialModel]()
        for index in 0 ..< 26 {
            let img = "FJ_Green_\(index)"
            let model = FJPushMaterialModel(iconImage: img, select: false)
            arr.append(model)
        }
        return arr
    }()
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        backgroundColor = .white
        subviews{
            collectionView
        }
        
        collectionView.fillContainer()
        let w = (CGFloat.screenWidth - 74.0) / 5.0
        let layout = UICollectionViewFlowLayout()
        layout.scrollDirection = .vertical
        layout.minimumLineSpacing = 10
        layout.minimumInteritemSpacing = 10
        layout.itemSize = CGSize(width: w, height: w)
        layout.sectionInset = UIEdgeInsets(top: 15.0, left: 15.0, bottom: 0, right: 15.0)
        collectionView.style { v in
            v.collectionViewLayout = layout
            v.backgroundColor = .clear
            v.register(FJPushMaterialsItemCell.self, forCellWithReuseIdentifier: FJPushMaterialsItemCell.description())
            v.showsHorizontalScrollIndicator = false
            v.delegate = self
            v.dataSource = self
        }
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
}

extension FJSelMaterialsView: UICollectionViewDelegate, UICollectionViewDataSource{
    
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        if type == 2 {
            return greenArray.count
        }
        return fruitArray.count
    }
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        
        guard let item = collectionView.dequeueReusableCell(withReuseIdentifier: FJPushMaterialsItemCell.description(), for: indexPath) as? FJPushMaterialsItemCell else { return UICollectionViewCell() }
        if type == 2 {
            item.model = greenArray[indexPath.row]
        }else{
            item.model = fruitArray[indexPath.row]
        }
        return item
    }
    
    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        
        let model: FJPushMaterialModel?
        if type == 2 {
            greenArray[indexPath.row].select = !greenArray[indexPath.row].select
            model = greenArray[indexPath.row]
        }else{
            fruitArray[indexPath.row].select = !fruitArray[indexPath.row].select
            model = fruitArray[indexPath.row]
        }
        let dict = ["img": model?.iconImage as Any,"type": type] as [String : Any]
        NotificationCenter.default.post(name: .selMaterialNoti, object: nil, userInfo: dict)
    }
}
